
var pageSize = 10,pageNum = 1,canScroll=false,isload;
var baseData = "&city="+localStorage.getItem("cityName") +"&hpWrite_token="+localStorage.getItem("hpWrite_token");


businessList();

// 获取商业列表
function businessList(data,load){
  var searchData = data;
  var isload = load ? load : false;
  searchData = searchData ? searchData : "";
  // listCount(baseData);
  var lastData = searchData + baseData +"&pageNum="+pageNum+"&pageSize="+pageSize; 
  $.ajax({
      type:'POST',
      url:address+'/businessBuildings/list.do?format=true',
      data:lastData,
      dataType:'json',
      success:function(result){
        if(result.status ==200 && result.data.list !=""){
            if(isload){
                $("#businessList").append(businesslistTPL(result.data));
              }else{
                $("#businessList").html(businesslistTPL(result.data));
              }
          canScroll =true;
        }else{
          canScroll =false;
          if(!isload){
            var html = '<div class="nodata">没有数据</div>';
            $("#businessList").html(html);
          }
        }
      },
      error:function(result){
        message(result.message)
      }
  })
};

// //滚动加载
$(window).scroll(function(){
    var scrollT = document.documentElement.scrollTop || document.body.scrollTop,
    clientH = $(window).height(),
    docH = $(document).height();
    var $loading = $(".loading");
    if(scrollT >= (docH - clientH) && canScroll==true){
      canScroll = false;
      isload = true;
      pageNum ++;
      $loading.show();
      //加载数据
      var data = $("#houseCDN").serialize();
      businessList(data,isload);
    }else{
      $loading.hide();
    }
})

// 商业列表模板
function businesslistTPL(data){
  var data = data.list,html ='',point = localStorage.getItem("point");
  if(point){
    point = point.split(",");
    var lat1 = Number(point[0]),lng1 = Number(point[1]);
  }
  $.each(data,function(index,info){
    var lat2 =info.latitude,lng2=info.longitude,
    distance = getDistance(lat1,lng1,lat2,lng2);
    distance = (distance/1000).toFixed(1);
    var img = info.image ? info.image+'&size=180x150' : noImg ;
    html += '<a class="db list p5 b-b" href="oldList-detail.html?id='+info.id+'">'+
        '<dl>'+
          '<dt><img src="'+ img +'" class="pic"></dt>'+
          '<dd>'+
            '<div class="name l-h20 black">'+info.name+'</div>'+
            '<div class="comment">'+
              '<span class="fr">'+info.price+'元'+
              '</span><span class="gray">'+info.decorationStr+'</span>'+
            '</div>'+
            '<p class="listAddress"><span class="fr gray"><i class="icon-gps"></i>'+distance+'km</span>'+info.address+'</p>'+
            '<div class="label orange">';
            var label = info.labelList;
            for(var i=0;i<label.length;i++){
              html+='<span>'+label[i]+'</span>';
            }
            html += '</div>'+
          '</dd>'+
        '</dl>'+
    '</a>';
  })
  return html;
}
 
//点击分类查询
$(".menu").on("click","li",function(){
  var $this = $(this),index = $this.index();
  $this.find(".icon-down").addClass("checked");
  $this.siblings().find(".icon-down").removeClass("checked");
  $(".menu-item").children().eq(index).show().siblings().hide();
  $(".coverDig,.menu-item").show();
});
$(".menu-item").on("click","p",function(){
    var $this = $(this),index = $this.index();
    $this.addClass("orange").siblings().removeClass("orange");
    $this.parent().siblings().find("ul").eq(index).show().siblings().hide();
});

$(".menu-item").on("click","li",function(){
    var $this = $(this),
    val = $this.attr("data"),
    type = $this.parent().attr("data-type");
    $("#classifyInput").find('input[name="'+type+'"]').val(val);
    var data = $("#houseCDN").serialize();
    businessList(data);
    $this.addClass("orange").siblings().removeClass("orange");
    $(".menu").find(".icon-down").removeClass("checked");
    $(".menu-item").hide();
    $(".coverDig").hide();
});

$("#houseCDN").on("keyup",".search",function(){
  var data = $("#houseCDN").serialize();
  businessList(data);
});

// 获取总条数
// function listCount(data){
//   $.ajax({
//       type:'POST',
//       url:address+'/oldBuildings/count.do',
//       data:data,
//       dataType:'json',
//       success:function(result){
//         if(result.status ==200){
//           $("#listCount").html('共'+result.data.total+'个楼盘');
//         }
//       }
//   })
// }

//获取分类 总价
~function classifyRentPrice(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listBusinessBuildingsPrice.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ='<li data="0">不限</li>';
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyAllPrice").html(html);
        }
      }
  })
}();

//获取分类 面积
~function classifyArea(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listBusinessBuildingsArea.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ="";
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyArea").html(html);
        }
      }
  })
}();

//获取分类 面积
~function classifyType(){
  $.ajax({
      type:'POST',
      url:address+'/housepropertyDataDictionary/listBusinessBuildingsType.do',
      data:{},
      dataType:'json',
      success:function(result){
        if(result.status ==200){
          var html ='<li data="0">不限</li>';
          $.each(result.data.list,function(index,info){
            html += '<li data="'+info.key+'">'+info.value+'</li>'
          })
          $("#classifyType").html(html);
        }
      }
  })
}();


